package cn.zmall.front.controller;

import cn.zmall.common.pojo.Result;
import cn.zmall.common.pojo.TreeNode;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.content.service.ContentService;
import cn.zmall.manager.dto.front.AllGoodsResult;
import cn.zmall.manager.dto.front.ProductDet;
import cn.zmall.manager.dto.front.SearchResult;
import cn.zmall.manager.pojo.TbPanel;
import cn.zmall.manager.pojo.TbPanelContent;
import cn.zmall.search.service.SearchService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author BinBini
 * @Date 2020/12/16 11:23
 * @Version 1.0
 */
@RestController
@Api(value = "商品展示")
public class GoodsController {

    @Reference
    private ContentService contentService;
    @Reference
    private SearchService searchService;

    @RequestMapping(value = "/goods/navList", method = RequestMethod.GET)
    @ApiOperation(value = "获取导航栏")
    public Result<List<TbPanelContent>> getNavList() {
        List<TbPanelContent> list = contentService.getNavList();
        return new ResultUtil<List<TbPanelContent>>().setData(list);
    }

    @RequestMapping(value = "/goods/home", method = RequestMethod.GET)
    @ApiOperation(value = "获取首页数据")
    public Result<List<TbPanel>> getHome() {
        List<TbPanel> list = contentService.getHome();
        return new ResultUtil<List<TbPanel>>().setData(list);
    }

    @RequestMapping(value = "/goods/productDet", method = RequestMethod.GET)
    @ApiOperation(value = "获取商品详情")
    public Result<ProductDet> getProductDet(Long productId) {
        ProductDet productDet = contentService.getProductDet(productId);
        return new ResultUtil<ProductDet>().setData(productDet);
    }

    @RequestMapping(value = "/goods/allGoods", method = RequestMethod.GET)
    @ApiOperation(value = "分页多条件获取全部商品")
    public Result<AllGoodsResult> getAllProduct(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "20") int size,
                                                @RequestParam(defaultValue = "") String sort,
                                                @RequestParam(defaultValue = "") Long cid,
                                                @RequestParam(defaultValue = "-1") int priceGt,
                                                @RequestParam(defaultValue = "-1") int priceLte){
        AllGoodsResult result = contentService.getAllProduct(page, size, sort, cid, priceGt, priceLte);
        return new ResultUtil<AllGoodsResult>().setData(result);
    }

    @RequestMapping(value = "/goods/search", method = RequestMethod.GET)
    @ApiOperation(value = "ES商品搜索")
    public Result<SearchResult> searchProduct(@RequestParam(defaultValue = "") String key,
                                              @RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "20") int size,
                                              @RequestParam(defaultValue = "") String sort,
                                              @RequestParam(defaultValue = "-1") int priceGt,
                                              @RequestParam(defaultValue = "-1") int priceLte){
        SearchResult result = searchService.search(key, page, size, sort, priceGt, priceLte);
        return new ResultUtil<SearchResult>().setData(result);
    }

    @RequestMapping(value = "/goods/recommend", method = RequestMethod.GET)
    @ApiOperation(value = "获取商品推荐板块")
    public Result<List<TbPanel>> getRecommendGoods() {
        List<TbPanel> list = contentService.getRecommendGoods();
        return new ResultUtil<List<TbPanel>>().setData(list);
    }

    @RequestMapping(value = "/goods/quickSearch", produces= "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ApiOperation(value = "快速搜索")
    public Result<String> getQuickSearch(@RequestParam(defaultValue = "") String key){
        String result = searchService.quickSearch(key);
        return new ResultUtil<String>().setData(result);
    }

    @RequestMapping(value = "/goods/cat/list", method = RequestMethod.GET)
    @ApiOperation(value = "通过父ID获取商品分类列表")
    public Result<List<TreeNode>> getItemCatList(@RequestParam(name="id",defaultValue = "0") int parentId) {
        List<TreeNode> result = contentService.getItemCatList(parentId);
        return new ResultUtil<List<TreeNode>>().setData(result);
    }

    @RequestMapping(value = "/goods/cat/random/list", method = RequestMethod.GET)
    @ApiOperation(value = "随机获取商品分类列表")
    public Result<List<TreeNode>> getRandomItemCatLIst() {
        List<TreeNode> result = contentService.getRandomItemCatLsit();
        return new ResultUtil<List<TreeNode>>().setData(result);
    }
}
